angular.module("module.goodsSendcard",[])
    .config(['$stateProvider',function ($stateProvider) {
        $stateProvider
            .state('home.goodsSendcard',{
                url:"/goodsSendcard",
                templateUrl:"goodsSendcard/goodsSendcard.html",
                controller:'GoodsSendcardController',
                controllerAs:'vm'
            })
    }])
    .controller('GoodsSendcardController',[function () {
        var vm=this;
        $('.gn_tabs a').click(function(e) {
            e.preventDefault();
            $(this).tab('show');
            var tmpl = e.target.hash.replace('#','');
            loadpage(tmpl);
        });
        function loadpage(tmpl, para){
            if (!tmpl) tmpl = 'brith';
            var tmplObj = $('#'+tmpl);
            var url = tmplObj.attr('url') || '';
            para = para||{};
            para['type'] = ({'brith':0,'jine':1,'cishu':2,'sex':3})[tmpl];
            $.getJSON(url, para, function(res){
                if (!res.succeed) return toast('获取数据失败，失败原因：'+res.message);
                if (tmpl=='jine') {
                    if (para.min)para.min/=100;
                    if (para.max)para.max/=100;
                }
                $.extend(res.data, para);
                res.data.type = tmpl;
                tmplObj.html(template('users_tmpl', res.data));
                if (tmpl=='cishu' || tmpl == 'jine'){
                    $('.datetime').daterangepicker({format: 'YYYY-MM-DD HH:mm',timePicker:true,
                        timePickerSeconds:true,timePickerIncrement:1,timePicker12Hour:false}, function(start, end) {
                        data.startTime = start.format('YYYY-MM-DD HH:mm');
                        data.endTime = end.format('YYYY-MM-DD HH:mm');
                        $(':input[name=startTime]').val(data.startTime);
                        $(':input[name=endTime]').val(data.endTime);
                    });
                }
                $('.search').off().on('click', function(){
                    var box = $('.active .memberbox');
                    var input = box.find(':input');
                    var paras = {};
                    $.each(input, function(){
                        var name = $(this).attr('name');
                        if (name){
                            paras[name]=$.trim($(this).val());
                        }
                    });
                    if (tmpl=='jine') {
                        if (paras.min)paras.min*=100;
                        if (paras.max)paras.max*=100;
                        if (paras.min < 1 || paras.max < 1) return toast('价格区间最小不能小于0.01元');
                    } else if(tmpl == 'cishu'){
                        if (paras.min < 1 || paras.max < 1) return toast('消费次数区间最小不能少于1次');
                    } else if (tmpl=='sex') {
                        if (paras.gender == '') return toast('请选择会员性别');
                    }
                    loadpage(tmpl, paras);
                });
                var checked = false;
                $('.checkAll').off().on('click', function(){
                    checked = !checked;
                    $('.active .from_table :checkbox').prop('checked', checked);
                });
                $('.send').off().on('click', function(){
                    winAlert({body:template('card_tmpl', data),title:'选择赠送的卡券', fun:function(){
                        var i = $('#card').val();
                        if (i=='') return toast('请选择已创建好的卡券');
                        var vo = data.cardList[i];
                        var checkbox = $('.active .from_table :checked');
                        var param = [];
                        if (checkbox.length<1) return toast('请选择要赠送的会员');
                        $.each(checkbox, function(){
                            param.push($(this).val());
                        });
                        if (param.length > vo.cardCount) return toast('卡券数量不够...');
                        return $.post('toSendCard',{openIds:param.join(','),cardNo:vo.cardNo}, function(rs){
                            try{rs=JSON.parse(rs);}catch(e){toast(e);}
                            if (!rs.succeed) return toast(rs.message);
                            toast('发送成功...');
                            return true;
                        });
                    }});
                });
            });
        }
        loadpage();
    }]);